﻿using System.ComponentModel;
using System.Windows;
using BrainTechLLC.WebCam;

namespace WebCamViewer
{
    /// <summary>
    /// Interaction logic for WebCamSettingsWindow.xaml
    /// </summary>
    public partial class WebCamSettingsWindow : Window
    {
        public static readonly DependencyProperty WebcamVideoControlProperty =
            DependencyProperty.Register("WebcamVideoControl", typeof(WebcamVideoControl), typeof(WebCamSettingsWindow), new PropertyMetadata(null, OnWebcamVideoControlPropertyChanged));

        public WebcamVideoControl WebcamVideoControl
        {
            get { return (WebcamVideoControl)GetValue(WebcamVideoControlProperty); }
            set { SetValue(WebcamVideoControlProperty, value); }
        }

        private static void OnWebcamVideoControlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebCamSettingsWindow source = d as WebCamSettingsWindow;
            source.UpdateWebcamVideoControl(e.NewValue as WebcamVideoControl, e.OldValue as WebcamVideoControl);
        }

        private void UpdateWebcamVideoControl(WebcamVideoControl viewModel, WebcamVideoControl viewModelOld)
        {
            WebcamVideoControl = viewModel;

            if (viewModelOld != null)
            {
                if (viewModelOld != viewModel)
                {

                }
            }

            this.DataContext = viewModel;
            this.propGrid.DataContext = viewModel;
            this.InvalidateVisual();
        }

        public WebCamSettingsWindow()
        {
            InitializeComponent();
            this.Closing += new CancelEventHandler(WebCamSettingsWindow_Closing);
        }

        void WebCamSettingsWindow_Closing(object sender, CancelEventArgs e)
        {
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }
        }
    }
}
